package com.ujiuye.job;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Set;

/**
 * @author: zwp
 * @version: 1.0
 * @create 2021-07-08 11:24
 */
@Component
public class ClearImgJob {

    @Resource
    private RedisTemplate<String,String> template;
    @Value("${uploadPath}")
    private String path;
    @Scheduled(cron = "0 52 11 ? * *")  //挑一个没有人上班的时间
    public void job(){
        SetOperations<String, String> opsForSet = template.opsForSet();
        //求两个集合的差集
        Set<String> difference = opsForSet.difference("SETMEAL_PIC_UPLOAD", "SETMEAL_PIC_DB");
        //判断差集是否为空，
        if (difference!=null && difference.size()>0){
            for (String pic : difference) {
                File file = new File(path+pic);
                file.delete();
            }
        }
        //清除redis集合中的数据
        template.delete("SETMEAL_PIC_UPLOAD");
        template.delete("SETMEAL_PIC_DB");
        System.out.println("正在清理图片");
    }
}
